//
//  LGUploadReportPhotoController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/24.
//  上传报告图片

#import "LGUploadReportPhotoController.h"
#import "LGPhotoPickerView.h"
@interface LGUploadReportPhotoController ()<LGPhotoPickerDelegate>
/**   */
@property (nonatomic , strong)LGPhotoPickerView *photoView;
/**   */
@property (nonatomic , strong)UIButton *uploadBtn;
/**   */
@property (nonatomic , assign)BOOL changed;
@end

@implementation LGUploadReportPhotoController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.changed = NO;
    self.navigationView.title = @"上传报告";
    [self.view addSubview:self.photoView];
    [self.view addSubview:self.uploadBtn];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self resetNavigationTheme];
}

-(void)backButtonAction{
    if(self.changed == YES){
        __weak typeof(self) weakSelf = self;
        [LGAlertTool alertWithTitle:@"放弃编辑" content:@"确认放弃编辑？" sureAction:^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }];
    }else{
        [self.navigationController popViewControllerAnimated:YES];
    }
}

-(void)setPhotoArray:(NSArray *)photoArray{
    _photoArray = photoArray;
    NSMutableArray *picArray = [NSMutableArray array];
    for (NSDictionary *tempDic in photoArray) {
        [picArray addObject:LGNSString(tempDic[@"image_url"])];
    }
    self.photoView.urlArray = picArray;
}

-(void)imagePickerHeightChanged:(CGFloat)height{
    self.photoView.frame = CGRectMake(viewPix(16), topBarHeight+viewPix(16), Screen_W-viewPix(32), height);
}


//上传照片
-(void)uploadPhotoAction{
    //保存过的网络图片
    NSMutableArray *oldArray = [NSMutableArray array];
    for (NSDictionary *tempDic in self.photoArray) {
        NSString *url = LGNSString(tempDic[@"image_url"]);
        if ([self.photoView.urlArray containsObject:url]) {
            [oldArray addObject:tempDic];
        }
    }
    
    if(self.photoView.addImageArray.count>0){
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        [RequestUtil uploadImage:@"/doctor/upload/image" parameters:@{} withSign:YES indexName:@"photo[]" imageArray:self.photoView.addImageArray success:^(NSInteger code, NSString *message, id returnData) {
            [MBProgressHUD hideHUDForView:self.view animated:YES];
            if(code == 200){
                NSArray *dataArray = returnData;
                NSMutableArray *photoArray = [NSMutableArray arrayWithArray:oldArray];
                for (NSDictionary *tempDic in dataArray) {
                    [photoArray addObject:@{@"img_id":@"0",@"image_uri":LGNSString(tempDic[@"url"]),@"image_url":LGNSString(tempDic[@"uri"])}];
                }
                if(self.uploadPhotoReportSuccess){
                    self.uploadPhotoReportSuccess(photoArray);
                }
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } failure:^(NSError *error) {
            [TooltipView showMessage:@"图片保存失败，请重试" offset:0];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }];
    }else if(oldArray.count>0){
        //只有网络图
        if(self.uploadPhotoReportSuccess){
            self.uploadPhotoReportSuccess(oldArray);
        }
        [self.navigationController popViewControllerAnimated:YES];
    }else{
        [TooltipView showMessage:@"请选择图片" offset:0];
    }
}


-(LGPhotoPickerView *)photoView{
    if(!_photoView){
        _photoView = [[LGPhotoPickerView alloc]initWithFrame:CGRectMake(viewPix(16), topBarHeight+viewPix(16), Screen_W-viewPix(32), viewPix(72))];
        _photoView.backgroundColor = [UIColor clearColor];
        _photoView.collectionView.backgroundColor = [UIColor clearColor];
        _photoView.delegate = self;
        _photoView.maxCount = 100;
    }
    return _photoView;
}


-(UIButton *)uploadBtn{
    if(!_uploadBtn){
        _uploadBtn = [UIButton buttonWithTitle:@"保存" topMargin:Screen_H-viewPix(66)-bottomSafeBarHeight/3.0];
        _uploadBtn.cornerRidus = viewPix(23);
        [_uploadBtn addTarget:self action:@selector(uploadPhotoAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _uploadBtn;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
